<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Exceptions\ValidationException;
use App\Http\Controllers\BaseController;
use App\Modules\Admin\Services\CulturalHeritageService;
use Illuminate\Http\Request;

class CulturalHeritageController extends BaseController
{
    public function __construct(CulturalHeritageService $culturalHeritageService)
    {
        $this->middleware('check.adminLogin')->except('index');

        $this->culturalHeritageService = $culturalHeritageService;
    }

    public function index()
    {
        $param = $this->validationParameter([
            'page|页码' => 'noRequire|number|default:1',
            'per_page|显示数量' => 'noRequire|number|default:30',
        ]);

        $data = $this->culturalHeritageService->getList($param);

        return $this->response->success($data);
    }

    public function show($id)
    {
        if (!$id) {
            throw new ValidationException('缺失参数');
        }

        $result = $this->culturalHeritageService->getInfo($id);

        return $this->response->success($result);
    }

    public function store(Request $request)
    {
        $param = $this->validationParameter([
            'title|标题' => 'require',
            'content|内容' => 'require',
            'image|图片' => 'require',
            'type|类型' => 'require|number|default:1',
            'desc|简介' => 'noRequire',
            'is_home|是否首页' => 'noRequire',
            'cover|首页封面图' => 'noRequire',
            'level|非遗级别' => 'noRequire',
            'sort|排序' => 'noRequire',
        ]);

        $param['publisher_uid'] = $request->user()->admin_uid ?? null;

        $result = $this->culturalHeritageService->add($param);

        return $this->response->success($result);
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        $param = $this->validationParameter([
            'title|标题' => 'require',
            'content|内容' => 'require',
            'image|图片' => 'require',
            'type|类型' => 'require|number|default:1',
            'desc|简介' => 'noRequire',
            'is_home|是否首页' => 'noRequire',
            'cover|首页封面图' => 'noRequire',
            'level|非遗级别' => 'noRequire',
            'sort|排序' => 'noRequire',
        ]);

        $param['publisher_uid'] = $request->user()->admin_uid ?? null;

        $result = $this->culturalHeritageService->update($param, $id);

        return $this->response->success($result);
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id');
        $result = $this->culturalHeritageService->delete($id);

        return $this->response->success($result);
    }
}
